package com.gehj.androidlibs.fragments;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gehj.androidlibs.R;

import org.jetbrains.annotations.NotNull;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private TextView textView;
    private Activity mActivity;
    public BlankFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        // IMPORTANT:  系统不按照以下的写法主要是利用BlankFragment回收以后可以恢复，静态变量不行；
        // BlankFragment.ARG_PARAM1 = "aaa";
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        textView = view.findViewById(R.id.tv1);
        textView.setText(mParam1);
        return view;
    }

    /**
     *
     * IMPORTANT: fragment是个普通类，只有add(),与Activityacttch上了才有生命周期
     *      所以重要的如果要调用里面的view，必须要在前面加判断，否则会报空；
     * @param title
     */

    public  void  changeTitle(String title){
        if (! isAdded()) return;
        textView.setText(title);
    }

    //IMPORTANT: 为了防止getActivity为空而使用的方法，有时候会有一定作用；
    @Override
    public void onAttach(@NonNull  Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }
}